package com.compass.mobile.framework.utils

import android.os.Build
import androidx.annotation.RequiresApi
import java.time.LocalDate
import java.util.Calendar

object PatientDataUtil {

    const val UNDERAGE :Int = 14

    /**
     * 检测是否为有效的身份证号
     */
     fun checkValidIdNumber(idNumber: String):Boolean {
        val pattern = "^\\d{17}[\\d|x|X]{1}$".toRegex()
        return pattern.matches(idNumber)
    }

    /**
     * 根据身份证号码获取到年龄
     */
//    @RequiresApi(Build.VERSION_CODES.O)
    fun getAgeByIdNumber(idNumber: String):Int {
        if(checkValidIdNumber(idNumber)){
//            if{Build.VERSION.SDK_INT>= Build.VERSION_CODES.O}{
//
//            }
            val currentYear = Calendar.getInstance().get(Calendar.YEAR);
//            val currentYear = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                LocalDate.now().year
//            } else {
//                Calendar.getInstance().get(Calendar.YEAR);
//            }
            val birthYear = idNumber.substring(6, 10).toInt()
            return currentYear - birthYear
        }
        return 0
    }

    /**
     * 是否为未成年，未成年的情况下需要校验监护人信息
     */
    fun isUnderage(cardNo:String):Boolean{
        if(checkValidIdNumber(cardNo)){
            return false
        }
        if(getAgeByIdNumber(cardNo) < UNDERAGE){
            return true
        }
        return false
    }
}